using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpcuaClient.Model;

namespace OpcuaClient.View
{
    public partial class Form_Construction : Form
    {
        public Construction Construction { get; set; }
        public bool Status { get; set; }
        public Form_Construction()
        {
            InitializeComponent();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbKey.Text) || string.IsNullOrWhiteSpace(tbName.Text)
                || string.IsNullOrWhiteSpace(tbFrequency.Text) || string.IsNullOrWhiteSpace(tbTopic.Text))
            {

                MessageBox.Show(this, "信息配置不完整，请检查!", "错误提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            InitConstruction();
            Status = true;
            Close();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Status = false;
            Close();
        }

        private void InitConstruction()
        {
            if (Construction == null)
            {
                Construction = new Construction();
            }
            if (string.IsNullOrWhiteSpace(tbKey.Text) || string.IsNullOrWhiteSpace(tbName.Text)
                || string.IsNullOrWhiteSpace(tbFrequency.Text) || string.IsNullOrWhiteSpace(tbTopic.Text))
            {

                MessageBox.Show(this, "信息配置不完整，请检查!", "错误提示！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Construction.Key = tbKey.Text;
            Construction.Name = tbName.Text;
            Construction.Frequency = int.Parse(tbFrequency.Text);
            Construction.Topic = tbTopic.Text;
        }

        public void Config(Construction construction = null)
        {
            if (construction != null)
            {
                Construction = construction;
            }

            if (Construction == null)
            {
                Construction = new Construction();
                return;
            }
            tbKey.Text = Construction.Key;
            tbName.Text = Construction.Name;
            tbFrequency.Text = Construction.Frequency.ToString();
            tbTopic.Text = Construction.Topic;
        }
    }
}
